package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired(required = false)
    private CategoryService categoryService;



    //session用来保存登录了的用户
    @Autowired
    private HttpSession session;

    /**
     * 添加菜品功能
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category){
        Long id = (Long) session.getAttribute("Employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.add(category);
        return R.success("添加成功");
    }

    /**
     * 菜品分页查询功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")  Integer pageSize){
        Page<Category> categoryPage = categoryService.find(page,pageSize);
        return R.success(categoryPage);
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        categoryService.delete(id);
        return R.success("删除成功");
    }


    /**
     * 修改菜品功能
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        Long id = (Long) session.getAttribute("Employee");
        category.setUpdateUser(id);
        categoryService.update(category);
        return R.success("修改成功");
    }

    /**
     * 导出Excel文件功能
     * @param response 响应对象可以获取输入流
     * @throws IOException
     */
    @GetMapping("/exportExcel")
    public void  exportExcel(HttpServletResponse response) throws IOException {
        //注意：如果是要做下载处理，必须通知浏览器处理文件的内容以文件下载的形式去处理
        response.setHeader("content-disposition","attachment;filename=category.xlsx"); //设置响应头

        //1. 读取模板输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");

        //2. 工作工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 通过工作簿得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //4. 得到第一行的内容，提取第一行的样式存储到集合中
        List<CellStyle> cellStyleList = new ArrayList<>();
        Row row = sheet.getRow(1);
        for (int i = 1; i <6 ; i++) {
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStyleList.add(cellStyle);
        }

        //5. 查询所有的类别信息，得到List集合。
        List<Category> categoryList = categoryService.find();

        //6. 遍历所有的类别信息，每一个类别信息对应一行内容。
        for (int i = 0; i < categoryList.size(); i++) {
            Category category = categoryList.get(i);
            //每一个类别信息对应一行内容。
            row = sheet.createRow(i+1);

            //设置单元格的内容
            Cell cell =  row.createCell(1);
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStyleList.get(0));


            cell =  row.createCell(2);
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStyleList.get(1));



            cell =  row.createCell(3);
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStyleList.get(2));


            cell =  row.createCell(4);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));
            cell.setCellStyle(cellStyleList.get(3));

            cell =  row.createCell(5);
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));
            cell.setCellStyle(cellStyleList.get(4));
        }

        //7. 把工作簿输出浏览器
        workbook.write(response.getOutputStream());
    }

    /**
     * 菜品展示
     * @param type
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> list(Integer type){
        R<List<Category>> result = categoryService.list(type);
        return result;
    }
}
